//
//  ViewController.m
//  BuildTestApp
//
//  Created by sr liu on 2024/3/5.
//

#import "ViewController.h"
#import <objc/message.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.view.backgroundColor = [UIColor cyanColor];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self createClass];
}


/*
 https://derekselander.github.io/dsdump/
 3.1 Objective-C Class List
 Using the Mach-O knowledge you’ve built up earlier, it’s quite easy to hunt for Objective-C classes that are built into an image. All you have to do is look for the __DATA_CONST.__objc_classlist (or __DATA.__objc_classlist if pre iOS-13) Mach-O section in an image.
 
 */


- (void)createClass {
    Class newClass = objc_allocateClassPair([NSError class], "RuntimeErrorSubclass", 0);
    class_addMethod(newClass, @selector(report), (IMP)ReportFunction, "v@:");
    objc_registerClassPair(newClass);
    
    id obj = [[newClass alloc] init];
    [obj performSelector:@selector(report)];
    
}

void ReportFunction(id self, SEL _cmd) {
    NSLog(@"This object is %p.", self);
    NSLog(@"Class is %@, and super is %@.", [self class], [self superclass]);
    
    Class currentClass = [self class];
    for (int i = 1; i < 5; i++)
    {
        NSLog(@"Following the isa pointer %d times gives %p", i, currentClass);
        currentClass = object_getClass(currentClass);
    }

    NSLog(@"NSObject's class is %p", [NSObject class]);
    NSLog(@"NSObject's meta class is %p", object_getClass([NSObject class]));
}
@end
